﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NicoView.ViewModel;

using GalaSoft.MvvmLight.Messaging;

using NicoView.Model.Service;
using NicoView.ViewModel.Message;
using GalaSoft.MvvmLight.Command;

namespace NicoView.View
{
    /// <summary>
    /// NicoViewIdInputControl.xaml の相互作用ロジック
    /// </summary>
    public partial class NicoViewIdInputControl : UserControl
    {
        public NicoViewIdInputControl()
        {
            InitializeComponent();

        }



        public new  NicoViewIdInputViewModel DataContext
        {
            get
            {
                
                return base.DataContext as NicoViewIdInputViewModel;
            }
        }


        private ICommand _connectCommand;
        public ICommand ConnectCommand
        {
            get
            {
                if (_connectCommand == null)
                {
                    _connectCommand = new RelayCommand(() =>
                    {
                        string id = "";
                        if (this.InputComboBox.SelectedIndex == -1)
                        {
                            id = this.InputComboBox.Text;
                        }
                        else
                        {
                            id = this.DataContext.ConnectSelections[this.InputComboBox.SelectedIndex].Id;
                        }
                        if (this.DataContext.ConnectCommand.CanExecute(null))
                        {
                            this.DataContext.InputId = id;
                            try
                            {
                                
                                this.DataContext.ConnectCommand.Execute(null);
                            }
                            finally
                            {
                                
                            }
                        }
                    });
                }

                return _connectCommand;
            }
        }

        private void InputComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



        private void IdInputControl_Loaded(object sender, RoutedEventArgs e)
        {
            _connectCommand = null;

            if (!DataContext.IsInDesignMode) Messenger.Default.Register<NicoViewAddSelectionMessage>(this, (args) =>
            {

                this.InputComboBox.SelectedItem = args.Model;
            });

            this.DataContext.Loded();
            


        }


    }
}
